﻿Imports FinanceiroBLL
Public Class PlanodeContasCON
    Dim objBLL As New PlanoContasBLL
    Dim objCtPag As New ContasaPagarFRM
    Public tipo As String
    Public objplano As New PlanoContasBLL
    Public ccccod As Integer
    Public ccusto As String
#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region
#Region "Cadastrar"
    Private Sub TSBCadastrar_Click(sender As System.Object, e As System.EventArgs) Handles TSBCadastrar.Click
        PlanoContasFRM.ShowDialog()
        carregagrid()
    End Sub
#End Region
#Region "Carrega Grid"
    Public Sub CarregaGrid()
        Dim dt As New DataTable
        dt = objBLL.buscar(tipo, 4) 'ira buscar somente os creditos
        DGVDados.DataSource = dt
        DGVDados.Refresh()
    End Sub
#End Region
#Region "ComboBox Filtro"
    Private Sub CBFiltro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs)
        TXTLocalizar.Clear()
        If (CBFiltro.Text = "Status") Then
            TXTLocalizar.Visible = False
        Else
            TXTLocalizar.Clear()
            TXTLocalizar.Visible = True
        End If
        If TXTLocalizar.Text = "" Then
            CarregaGrid()
        End If
    End Sub
#End Region
#Region "Voltar"
    Private Sub TSBVoltar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub
#End Region
#Region "Keypress Localizar"
    Private Sub TXTLocalizar_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTLocalizar.KeyPress
        If CBFiltro.SelectedIndex = 0 Or CBFiltro.SelectedIndex = 3 Or CBFiltro.SelectedIndex = 4 Then
            If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
                e.Handled = True
            End If
        End If
    End Sub
#End Region

#Region "Carrega Grid"
    Private Sub PlanodeContasCON_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CarregaGrid()
        CBFiltro.SelectedIndex = 0
        CBFiltro.Focus()
    End Sub
#End Region

    Private Sub TXTLocalizar_TextChanged_1(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        Select Case CBFiltro.Text
            Case "Código"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, 0)
            Case "Descrição"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, 1)
            Case "Status"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, 2)
            Case "Tipo"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, 3)
            Case "Status"
                DGVDados.DataSource = objBLL.buscar(1, 5)
        End Select
        If TXTLocalizar.Text = "" Then
            DGVDados.DataSource = objBLL.buscar("", 9)
        End If
        DGVDados.Refresh()
    End Sub
#Region "Selected Index Changed"
    Private Sub CBFiltro_SelectedIndexChanged_1(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        TXTLocalizar.Clear()
        If (CBFiltro.Text = "Status") Then
            TXTLocalizar.Visible = False
            CBStatus.Visible = True
            CBStatus.SelectedIndex = 0
        Else
            TXTLocalizar.Clear()
            TXTLocalizar.Visible = True
            CBStatus.Visible = False
        End If
        If TXTLocalizar.Text = "" Then
            CarregaGrid()
        End If
    End Sub
#End Region
#Region "Voltar"
    Private Sub TSBVoltar_Click_1(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
    End Sub
#End Region
    Private Sub CBStatus_SelectedIndexChanged_1(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        Dim sta As Integer = 0
        If (CBStatus.Text = "Ativo") Then sta = 1
        If (CBStatus.Text = "Inativo") Then sta = 2
        DGVDados.DataSource = objBLL.buscar(sta, 2)
        DGVDados.Refresh()
    End Sub

    Private Sub DGVDados_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDados.CellDoubleClick
        Try

            '  If tipo = "ret" Then
            Dim indice As Integer = -1
            'Percorre O DataGridView Verificando a Linha selecionada
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("ccc_codigo") Is Nothing) Then
                    'Guarda A Posição Selecionada
                    indice = linha.Index
                End If
            Next
            If (indice <> -1) Then
                If (DGVDados.Rows(indice).Cells("sta_codigo").Value = 1) Then
                    ccccod = DGVDados.Rows(indice).Cells("ccc_codigo").Value.ToString()
                    ccusto = DGVDados.Rows(indice).Cells("ccc_descricao").Value.ToString()
                    '             ContasPagarFRM.codCC = 
                    Me.Close()
                Else
                    Throw New Exception("Conta de Centro-Custo Excluído!")
                End If
            End If
            '  End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class